﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using IFServerDLL.Broker;

namespace IFServerDLL.Model
{
    [DataContract]
    public class AccountBase
    {
        public AccountBase()
        {

        }
        public AccountBase(decimal currMargin)
        {
            CurrMargin = currMargin;
        }
        [DataMember]
        public decimal CurrMargin{get;set;}
        [DataMember]
        public decimal Available { get; set; }
        [DataMember]
        public decimal Commission { get; set; }

        [DataMember]
        public decimal CloseProfit { get; set; }

        [DataMember]
        public decimal MarketProfit { get; set; }

        [DataMember]
        public decimal OrderFrozenCash { get; set; }

        [DataMember]
        public decimal OrderFrozenCommssion { get; set; }
        [DataMember]
        public decimal TodayTradeCommssion { get; set; }
        [DataMember]
        public decimal TradeOnHandCash { get; set; }

        public void Update(UserOrderTrade user)
        {
            MarketProfit = user.ListTradeOnhand.Sum(p => p.CloseProfit);
            CloseProfit = user.ListTradeGroup.Sum(p => p.SumCloseProfit);
            OrderFrozenCash = user.GetOrderOnhand().Sum(p => p.FrozenCash);
            OrderFrozenCommssion = user.GetOrderOnhand().Sum(p => p.FrozenCommission);
            TradeOnHandCash = user.ListTradeOnhand.Sum(p => p.SumCash);
            TodayTradeCommssion = user.TradeHistory.Sum(p => p.Commission);
            Commission = OrderFrozenCommssion + TodayTradeCommssion;
            Available = CurrMargin - Commission - OrderFrozenCash - TradeOnHandCash + CloseProfit;
            if (Available < 0 || OrderFrozenCash < 0)
            {
                throw new Exception("available can not less then zero.");
            }
        }
        




        
    }
}
